/*
ECS386: Introduction to Embedded Systems
Processing code for spirometer project
Keerati Suibkitwanchai ID:5422780395
Siwalee Choilek ID:5422780536
*/

import processing.serial.*;      //load a library processing.serial
Serial serialPort;               //serial port

//Set previous values for each parameter
int previousPosition  = 0;       
float previousHeight = 0.0;
float previousInputNumber = 0.0;

//set the initial value of current position in x-axis
int currentPosition = 0;         

void setup (){
  size(700,700);                                              //set the size of window       
  println(Serial.list());                                     //list all available serial ports
  serialPort = new Serial(this, Serial.list()[0], 9600);      //open the port using the baud rate corresponding to Arduino:
  serialPort.bufferUntil('\n');                               //while a newline charcter is obtained, the serialEvent() is created
  background(0);                                              //set the initial background as black
}

void draw(){
//  
}

void serialEvent(Serial serialPort){
  String inputString = serialPort.readStringUntil('\n');      //obtain the input string from the port
  int v = inputString.indexOf("V");     
  
  //Volume case  
  if (v==0){
    inputString = inputString.substring(8);                    //clear the string "Volume: " out to pick up only the value of volume
    if (inputString != null) {     
      inputString = trim(inputString);                         //remove whitespace characters
      float inputNumber = float(inputString);                  //convert the string to the number value
      if(inputNumber-previousInputNumber < 0){                 //when the volume plot goes from the highest plot to the initial (offset) value 
          print("Volume = ");
          println(previousInputNumber);                        //the previous value is the highest plot which is the total volume 
      }
      previousInputNumber = inputNumber;
      inputNumber = map(inputNumber, 0, 6, 0, height);         //map the input number to the size of window
  
      //Draw a line
      stroke(255,255,255);                                                                    //line with white colour
      strokeWeight(2);                                                                        //line with weight of 2
      line(previousPosition, previousHeight, currentPosition, height-inputNumber);            //draw a line
      previousPosition = currentPosition;                                                     //move the current position to the previous position
      previousHeight = height-inputNumber;                                                    //move the current height to the previous height
      
      //At the edge (last position)
      if (currentPosition >= width) {
        currentPosition = 0;                                    //set back to the initial value
        previousPosition = 0;                                   //set back to the initial value
        background(0);                                          //clear to the black screen
      } 
      else {
        currentPosition=currentPosition+10;                     //increase the position by 10 (to see the plot clearer)
      }
    }
  }
}


